package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.myapplication.util.ToastUtil;
import com.example.myapplication.util.UserAuthUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // 声明控件
    private Button mBtnLogin;
    private Button mBtnSignUp;
    private EditText mEtUser;
    private EditText mEtPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 找到控件
        mBtnLogin = findViewById(R.id.btn_login);
        mBtnSignUp = findViewById(R.id.btn_signup);
        mEtUser = findViewById(R.id.et_1);
        mEtPassword = findViewById(R.id.et_2);


        // 跳转注册页面
        mBtnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = null;
                intent = new Intent(MainActivity.this, SignUpActivity.class);
                startActivity(intent);
            }
        });

        // V1:实现直接跳转
        /*mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = null;
                intent = new Intent(MainActivity.this, FunctionActivity.class);
                startActivity(intent);
            }
        });*/

        // V2:匹配成功后跳转
        mBtnLogin.setOnClickListener(this) ;
    }

    public void onClick(View v){
        // 获取输入的用户名密码
        String username = mEtUser.getText().toString();
        String password = mEtPassword.getText().toString();
        // 设置登录的弹出内容
        String ok = "恭喜您 登录成功！";
        String fail = "用户名或密码有误，请重新登录！";


        Intent intent = null;

        //验证用户名密码
        if(UserAuthUtil.checkUserCredentials(this,username,password)){
            //toast 普通
            //Toast.makeText(getApplicationContext(),ok,Toast.LENGTH_SHORT).show();
            //封装好的类
            ToastUtil.showMsg(MainActivity.this,ok);

            //跳转到欢迎页面
            intent = new Intent(MainActivity.this, SlideActivity.class);
            startActivity(intent);
        }
        else {
            //不正确
            //toast 提升版
            /*Toast toastCenter = Toast.makeText(getApplicationContext(),fail,Toast.LENGTH_SHORT);
            toastCenter.setGravity(Gravity.CENTER,0,0);
            toastCenter.show();*/
            ToastUtil.showMsg(MainActivity.this,fail);
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }
}